#include "Common.h"

#include "qlabel.h"
#include "qmimedatabase.h"
#include "qmimetype.h"
#include "qnamespace.h"
#include "qsizepolicy.h"

#include <QDebug>
#include <QIcon>
#include <QStyle>
#include <QApplication>
#include <QSizePolicy>
#include <QStyleFactory>

enum Weekday_FULL {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

enum Weekday_SHORT {
    Mon, // Monday
    Tue, // Tuesday
    Wed, // Wednesday
    Thu, // Thursday
    Fri, // Friday
    Sat, // Saturday
    Sun  // Sunday
};

enum Month {
    January,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December
};

QString Functions::WeekE2C(const QString &English)
{
    QString week;
    if(English == QString("Monday"))
    {
        return "星期一";
    }
    if(English == QString("Tuesday"))
    {
        return "星期二";
    }
    if(English == QString("Wednesday"))
    {
        return "星期三";
    }
    if(English == QString("Thursday"))
    {
        return "星期四";
    }
    if(English == QString("Friday"))
    {
        return "星期五";
    }
    if(English == QString("Saturday"))
    {
        return "星期六";
    }
    if(English == QString("Sunday"))
    {
        return "星期日";
    }
    return English;
}


bool Functions::getAVType(const QString &AVFile)
{
    QMimeDatabase MindDB;
    QMimeType MinTp = MindDB.mimeTypeForFile(AVFile);
    bool isAudio = MinTp.name().startsWith("audio/");
    bool isVideo = MinTp.name().startsWith("video/");
    if(isAudio || isVideo)
    {
        return true;
    }
    return false;
}


void Functions::sleep(unsigned long s)
{
    QThread::sleep(s);
}

void Functions::msleep(unsigned long ms)
{
    QThread::msleep(ms);
}

void Functions::usleep(unsigned long us)
{
    QThread::usleep(us);
}

std::string Functions::toBit(const void* data, size_t size)
{
    auto bin_data = [](const void* data,size_t size) -> std::string
    {
        std::string binStr;
        const unsigned char* ptr = reinterpret_cast<const unsigned char*>(data);
        for (size_t i = 0; i < size; ++i)
        {
            for (size_t j = 0; j < 8; ++j)
            {
                binStr = ((ptr[i] >> j) & 1 ? '1': '0') + binStr;
            }
        }
        return binStr;
    };

    std::string result,binStr = bin_data(data,size);
    int groupSize = 4;
    int count = 0;
    for (int i = binStr.length() - 1;i >= 0; --i)
    {
        result = binStr[i] + result;
        count++;
        if(count % groupSize == 0 && i > 0)
        {
            result = " " + result;
        }
    }
    std::cout << result << std::endl;
    return result;
}